package hot100.logos31To40;

import hot100.datastruct.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author logos
 * date 2025/7/13 12:52
 * @version v1.0
 * @Package hot100.logos31To40
 */
public class logos36 {

    /**
     * 36. 二叉树的中序遍历
     * https://leetcode.cn/problems/binary-tree-inorder-traversal/description/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution1 {
        ArrayList<Integer> ans = new ArrayList<>();

        private void solve(TreeNode root) {
            if (root == null) {
                return;
            }
            solve(root.left);
            ans.add(root.val);
            solve(root.right);
        }

        public List<Integer> inorderTraversal(TreeNode root) {
            solve(root);
            return ans;
        }
    }

    class Solution2 {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> ans = new ArrayList<>();
            TreeNode[] st = new TreeNode[105];
            int r = 0;
            while (r != 0 || root != null) {
                if (root != null) {
                    st[r++] = root;
                    root = root.left;
                } else {
                    root = st[--r];
                    ans.add(root.val);
                    root = root.right;
                }
            }
            return ans;
        }
    }
}
